﻿global using Microsoft.AspNetCore.Http;
global using Microsoft.AspNetCore.Mvc.Rendering;
global using Microsoft.AspNetCore.Mvc.TagHelpers;
global using Microsoft.AspNetCore.Mvc.ViewFeatures;
global using Microsoft.AspNetCore.Razor.TagHelpers;

global using WXWorkSvc.Common.Extensions;

namespace WXWorkSvc.Common.TagHelpers;
[HtmlTargetElement("desk-input", TagStructure = TagStructure.WithoutEndTag)]
public class DeskInputTagHelper : TagHelper
{
    [HtmlAttributeNotBound]
    [ViewContext]
    public ViewContext ViewContext { get; set; } = default!;
    private readonly IHtmlGenerator _HtmlGenerator;

    public DeskInputTagHelper(IHtmlGenerator generator)
    {
        _HtmlGenerator = generator;
    }

    [HtmlAttributeName(DictionaryAttributePrefix = "asp-for-")]
    public Dictionary<string, ModelExpression> Fors { get; set; } = new();
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-hidden-")]
    public Dictionary<string, ModelExpression> Hiddens { get; set; } = new();
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-readonly-")]
    public Dictionary<string, string> ReadOnlys { get; set; } = new();
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-select-")]
    public Dictionary<string, IEnumerable<SelectListItem>> SelectItems { get; set; } = new();

    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        output.TagName = "div";
        output.TagMode = TagMode.StartTagAndEndTag;
        output.Attributes.Add("name", "自动创建");


        var tagValidationSummary = _HtmlGenerator.GenerateValidationSummary(ViewContext);
        if (tagValidationSummary != null)
            output.Content.AppendHtml(tagValidationSummary);
        foreach (var f in Hiddens.Values)
        {
            output.Content.AppendHtml(_HtmlGenerator.GenerateHidden(ViewContext, f.ModelExplorer, f.Name, f.Model, false, null));

        }



        foreach (var kv in Fors)
        {
            var f = kv.Value;

            var tagRowDiv = new TagBuilder("div");
            tagRowDiv.Attributes.Add("id", "Div" + kv.Key);
            tagRowDiv.AddCssClass("mb-3 row");

           
            var tagLabel = f.GenerateLabel("col-sm-2 col-form-label text-end");

            if (SelectItems.Remove(kv.Key, out var selectList))
            {

                var tagInput = _HtmlGenerator.GenerateSelect(ViewContext, f.ModelExplorer, null, f.Name, selectList, false, new { @class = "form-control" });
                var tagInputContainer = new TagBuilder("div");
                tagInputContainer.AddCssClass("col-sm-10");
                tagInputContainer.InnerHtml.SetHtmlContent(tagInput);

                tagRowDiv.InnerHtml.SetHtmlContent(tagLabel);
                tagRowDiv.InnerHtml.AppendHtml(tagInputContainer);
            }
            else if (f.Metadata.ModelType.Name == "Boolean")
            {
                tagLabel.MergeAttribute("class", "form-check-label", true);
                var tagInput = _HtmlGenerator.GenerateCheckBox(ViewContext, f.ModelExplorer, f.Name, (bool)f.Model, new { @class = "form-check-input" });

                var tagInnerDiv = new TagBuilder("div");
                tagInnerDiv.AddCssClass("form-check");
                tagInnerDiv.InnerHtml.AppendHtml(tagInput);
                tagInnerDiv.InnerHtml.AppendHtml(tagLabel);

                var tagOutterDiv = new TagBuilder("div");
                tagOutterDiv.AddCssClass("col-sm-10 offset-sm-2");
                tagOutterDiv.InnerHtml.SetHtmlContent(tagInnerDiv);

                tagRowDiv.InnerHtml.SetHtmlContent(tagOutterDiv);
            }
            else
            {
                kv.Value.GetHtmlAttributes(out var htmlAttributes, out var format);

                var tagInput = _HtmlGenerator.GenerateTextBox(ViewContext, f.ModelExplorer, f.Name, f.Model, format, htmlAttributes);


                tagInput.Attributes.Add("class", "form-control");
                if (ReadOnlys.Remove(kv.Key, out var readonlyFlag) && (readonlyFlag == "" || readonlyFlag == "True"))
                    tagInput.Attributes.Add("readonly", "readonly");

                var tagInputContainer = new TagBuilder("div");
                tagInputContainer.AddCssClass("col-sm-10");
                tagInputContainer.InnerHtml.SetHtmlContent(tagInput);

                tagRowDiv.InnerHtml.SetHtmlContent(tagLabel);
                tagRowDiv.InnerHtml.AppendHtml(tagInputContainer);

            }


            output.Content.AppendHtml(tagRowDiv);

        }

    }
}

